package com.zhugang.week05;

/**
 * @program algorithms
 * @description: findMin2
 * @author: chanzhugang
 * @create: 2022/07/12 21:32
 */
public class FindMin2 {

    /**
     * 153 寻找旋转排序数组中的最小值
     *
     * @param nums
     * @return
     */
    public int findMin(int[] nums) {
        // 举例写代码
        // 1 2 3 4 5 6 7
        // 4 5 6 7 1 2 3
        if (nums[0] < nums[nums.length - 1]) {
            return nums[0];
        }

        int low = 0;
        int high = nums.length - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (low == high) {
                return nums[low];
            }
            // 命中
            if (mid != 0 && nums[mid] < nums[mid - 1]) {
                return nums[mid];
            } else if (nums[mid] > nums[high]) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
}